<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 7.2.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/ME.ico">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/ME.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/ME.ico">
  <link rel="mask-icon" href="/images/ME.ico" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"www.zhoubencheng.com","root":"/","scheme":"Pisces","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":true,"style":"mac"},"back2top":{"enable":true,"sidebar":false,"scrollpercent":true},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="MyBatis简介：MyBatis是支持定制化SQL、存储过程以及高级映射的持久层框架，该框架几乎避开了所有JDBC代码。Mybatis可以对其自身配置和原生Map映射使用xml注解，并且可以将java中的接口和POJOs（Plain Old Java Object）映射为数据库中的记录">
<meta property="og:type" content="article">
<meta property="og:title" content="MyBatis">
<meta property="og:url" content="http://www.zhoubencheng.com/2024/07/02/MyBatis/index.html">
<meta property="og:site_name" content="Benson&#39;s blog">
<meta property="og:description" content="MyBatis简介：MyBatis是支持定制化SQL、存储过程以及高级映射的持久层框架，该框架几乎避开了所有JDBC代码。Mybatis可以对其自身配置和原生Map映射使用xml注解，并且可以将java中的接口和POJOs（Plain Old Java Object）映射为数据库中的记录">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://i.postimg.cc/NFRYHcps/image.png">
<meta property="og:image" content="https://i.postimg.cc/RVzvzWZN/image.jpg">
<meta property="article:published_time" content="2024-07-02T15:35:17.000Z">
<meta property="article:modified_time" content="2024-08-21T14:53:08.042Z">
<meta property="article:author" content="Benson">
<meta property="article:tag" content="Java">
<meta property="article:tag" content="SQL">
<meta property="article:tag" content="后端">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://i.postimg.cc/NFRYHcps/image.png">

<link rel="canonical" href="http://www.zhoubencheng.com/2024/07/02/MyBatis/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>MyBatis | Benson's blog</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>


<style>.github-emoji { position: relative; display: inline-block; width: 1.2em; min-height: 1.2em; overflow: hidden; vertical-align: top; color: transparent; }  .github-emoji > span { position: relative; z-index: 10; }  .github-emoji img, .github-emoji .fancybox { margin: 0 !important; padding: 0 !important; border: none !important; outline: none !important; text-decoration: none !important; user-select: none !important; cursor: auto !important; }  .github-emoji img { height: 1.2em !important; width: 1.2em !important; position: absolute !important; left: 50% !important; top: 50% !important; transform: translate(-50%, -50%) !important; user-select: none !important; cursor: auto !important; } .github-emoji-fallback { color: inherit; } .github-emoji-fallback img { opacity: 0 !important; }</style>
</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">Benson's blog</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">最好的安排</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

  <a href="https://github.com/ZhouBencheng" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://www.zhoubencheng.com/2024/07/02/MyBatis/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/ME.ico">
      <meta itemprop="name" content="Benson">
      <meta itemprop="description" content="Share my thoughts with you">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Benson's blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          MyBatis
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2024-07-02 23:35:17" itemprop="dateCreated datePublished" datetime="2024-07-02T23:35:17+08:00">2024-07-02</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2024-08-21 22:53:08" itemprop="dateModified" datetime="2024-08-21T22:53:08+08:00">2024-08-21</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/JavaWeb/" itemprop="url" rel="index"><span itemprop="name">JavaWeb</span></a>
                </span>
            </span>

          
            <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span id="busuanzi_value_page_pv"></span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <blockquote>
<p>MyBatis简介：MyBatis是支持定制化SQL、存储过程以及<strong>高级映射</strong>的持久层框架，该框架几乎避开了所有JDBC代码。Mybatis可以对其自身配置和原生Map映射使用xml注解，并且可以将java中的<strong>接口</strong>和<strong>POJOs</strong>（Plain
Old Java Object）映射为<strong>数据库中的记录</strong></p>
</blockquote>
<span id="more"></span>
<h2 id="添加依赖">添加依赖</h2>
<p>我们将在<strong>Maven项目配置文件</strong><code>pom.xml</code>中添加本项目对<code>MyBatis</code>的依赖</p>
<blockquote>
<p>pom.xml (Project Object
Model)项目对象文件，是Maven工程的基本单元。在POM文件中，我们一般指定以下配置</p>
<ul>
<li>项目依赖
<code>&lt;dependencies&gt; &lt;/dependencies&gt;</code></li>
<li>插件 <code>&lt;plugin&gt; &lt;/plugin&gt;</code>
插件是Maven构建项目过程中扩展功能的组件</li>
<li>项目版本</li>
</ul>
<p>等信息</p>
</blockquote>
<figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependencies</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>mysql<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>mysql-connector-java<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">version</span>&gt;</span>8.0.29<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.mybatis<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>mybatis<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">version</span>&gt;</span>3.2.2<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependencies</span>&gt;</span></span><br></pre></td></tr></tbody></table></figure>
<p>在上述代码中，我们添加两个依赖，每项依赖使用三个字段进行描述，分别表示如下含义</p>
<ul>
<li><code>&lt;groupid&gt;</code>：指定工程组，一个工程组包含一类相关联的项目</li>
<li><code>&lt;artifactid&gt;</code>：<strong>制品ID</strong>，用于指定唯一的依赖名（项目名称）</li>
<li><code>&lt;version&gt;</code>：指定依赖版本</li>
</ul>
<h2 id="配置mybatis">配置MyBatis</h2>
<p>在<code>resources</code>文件夹下添加<code>MyBatis</code>配置文件<code>mybatis-config.xml</code>，在该文件中我们对本项目使用到的<code>MyBatis</code>进行<strong>全局配置</strong>。该文件中，我们将设置本项目对数据库的<strong>连接实例</strong>(DataSource)和决定<strong>事务范围</strong>和控制方式的事务管理器</p>
<blockquote>
<p>事务：在数据库管理系统中，事务(Transaction)是一组逻辑上相关的操作单元，其要么成功执行，要么执行失败并回滚，从而保证数据的一致性和完整性</p>
</blockquote>
<figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version=<span class="string">"1.0"</span> encoding=<span class="string">"UTF-8"</span> ?&gt;</span></span><br><span class="line"><span class="meta">&lt;!DOCTYPE <span class="keyword">configuration</span></span></span><br><span class="line"><span class="meta">	<span class="keyword">PUBLIC</span> <span class="string">"-//mybatis.org//DTD Config 3.0//EN"</span></span></span><br><span class="line"><span class="meta">	<span class="string">"http://mybatis.org/dtd/mybatis-3-config.dtd"</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">configuration</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">environments</span> <span class="attr">default</span>=<span class="string">"development"</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">environment</span> <span class="attr">id</span>=<span class="string">"development"</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">transactionManager</span> <span class="attr">type</span>=<span class="string">"JDBC"</span>/&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">dataSource</span> <span class="attr">type</span>=<span class="string">"POOLED"</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"driver"</span> <span class="attr">value</span>=<span class="string">"${driver}"</span>/&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"url"</span> <span class="attr">value</span>=<span class="string">"${url}"</span>/&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"username"</span> <span class="attr">value</span>=<span class="string">"${username}"</span>/&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"password"</span> <span class="attr">value</span>=<span class="string">"${password}"</span>/&gt;</span></span><br><span class="line">            <span class="tag">&lt;/<span class="name">dataSource</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">environment</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">environments</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">mappers</span>&gt;</span></span><br><span class="line">    	<span class="tag">&lt;<span class="name">mapper</span> <span class="attr">resource</span>=<span class="string">"org/mybatis/example/BlogMapper.xml"</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">mappers</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">configuration</span>&gt;</span></span><br></pre></td></tr></tbody></table></figure>
<ul>
<li>头部黑体部分XML声明，用于验证XML文档的正确性。<code>DOCTYPE</code>用于指定XML文件类型，<code>DTD</code>为文档类型定义，用于约束XML文件的编写规范</li>
<li><code>enviroment</code>元素中指定<strong>事务管理/transactionManager</strong>和<strong>连接池/dataSource</strong>的配置</li>
<li><code>mappers</code>元素指定一组<code>mapper</code>映射器</li>
</ul>
<p>在本项目中，对<code>dataSource</code>元素的属性需要进行如下设置</p>
<figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">&lt;--! 第一个属性设置MySQL的JDBC驱动 --&gt;</span><br><span class="line"><span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"driver"</span> <span class="attr">value</span>=<span class="string">"com.mysql.cj.jdbc.Driver"</span>/&gt;</span></span><br><span class="line">&lt;--! 第二个属性设置数据库连接的URL，其中?后设置连接参数 --&gt;</span><br><span class="line"><span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"url"</span> <span class="attr">value</span>=<span class="string">"jdbc:mysql://127.0.0.1:3306/ben?useUnicode=true<span class="symbol">&amp;amp;</span>characterEncodeing=utf8"</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"username"</span> <span class="attr">value</span>=<span class="string">"root"</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"password"</span> <span class="attr">value</span>=<span class="string">"zhoubecheng123@"</span>/&gt;</span></span><br></pre></td></tr></tbody></table></figure>
<h2 id="sql高级映射">SQL高级映射</h2>
<p>每个<code>MyBatis</code>应用程序基本都满足一下使用流程</p>
<p><img src="https://i.postimg.cc/NFRYHcps/image.png"></p>
<h3 id="会话连接">会话连接</h3>
<p>能够获取mysql数据库的连接池，关键在于使用<code>sqlSessionFactoryBuilder</code>函数工具构建<code>sqlSessionFactory</code>对象实例，而<code>sqlSessionFactoryBuilder</code>函数的逻辑则从资源配置文件<code>mybatis-config.xml</code>文件中获取，以下是创建<code>sqlSessionFactory</code>实例的<strong>标准</strong>代码</p>
<figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">String</span> <span class="variable">resource</span> <span class="operator">=</span> <span class="string">"mybatis-config.xml"</span>;</span><br><span class="line"><span class="type">InputStream</span> <span class="variable">inputStream</span> <span class="operator">=</span> Resources.getResourceAsStream(resource);</span><br><span class="line"><span class="type">SqlSessionFactory</span> <span class="variable">sqlSessionFactory</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">SqlSessionFactoryBuilder</span>().build(inputStream);</span><br></pre></td></tr></tbody></table></figure>
<p>注意在上述创建实例的流程中，<code>getResourceAsStream</code>函数会抛出一个异常我们必须处理——当<code>resource</code>指向的资源配置文件查询不到时，会抛出<code>IOException</code>错误，在测试函数中我们使用<code>throws</code>关键字将异常抛出，将由上一次处理</p>
<h3 id="获取会话实例">获取会话实例</h3>
<p><code>MyBatis</code>中，<code>sqlSession</code>是其核心数据结构，它代表和数据库的一次会话，通过如下方法构建一个<code>sqlSession</code>对象</p>
<figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">SqlSession</span> <span class="variable">sqlSession</span> <span class="operator">=</span> sqlSessionFactory.openSession(<span class="literal">true</span>);</span><br></pre></td></tr></tbody></table></figure>
<p><code>openSession</code>创建函数的参数<code>true</code>的含义是开启<strong>自动提交</strong>，意味着在执行完SQL语句后，事务会自动提交，不需要手动调用<code>sqlSession.commit()</code>方法</p>
<blockquote>
<p>关于<strong>事务</strong>和<strong>事务提交</strong></p>
<ul>
<li>事务具有ACID四个重要的属性
<ul>
<li>Atom原子性：事务是不可分割的工作过程，事务中的操作要么全部完成，要么全部不完成</li>
<li>Consistency一致性：事务必须使数据库从一个状态转化到另一个状态</li>
<li>Isolation隔离性：一个事务的执行不会受到其他事务的影响</li>
<li>Durability持久性：一旦事务完成，其执行结果会永久的保存在数据库中</li>
</ul></li>
<li>事务提交，指的是将事务的操作结果永久的保存在数据库中，如果事务提交成功，那么事务的所有结果都会永久的反映在数据库中；如果提交事务之前发生了任何错误，那么事务中的所有更改都会被撤销，数据库回到事务开始之前的状态</li>
</ul>
</blockquote>
<h3 id="mapper接口实现">Mapper接口实现</h3>
<blockquote>
<p>注意：</p>
<ul>
<li>在定义接口Mapper时，该接口的名称就对应于等会儿.xml映射实现文件的名称，两者同名</li>
<li>每一个为接口实现映射逻辑的xml文件都需要在<code>MyBatis</code>全局配置文件中注册，注册地点是<code>mappers</code>标签</li>
</ul>
</blockquote>
<p>以下是一个SQL单记录查询的实现</p>
<figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version=<span class="string">"1.0"</span> encoding=<span class="string">"utf-8"</span> ?&gt;</span></span><br><span class="line"><span class="meta">&lt;!DOCTYPE <span class="keyword">mapper</span></span></span><br><span class="line"><span class="meta">        <span class="keyword">PUBLIC</span> <span class="string">"-//mybatis.org//DTD Mapper 3.0//EN"</span></span></span><br><span class="line"><span class="meta">        <span class="string">"https://mybatis.org/dtd/mybatis-3-mapper.dtd"</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">mapper</span> <span class="attr">namespace</span>=<span class="string">"com.ben.mapper.ClazzMapper"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">"selectById"</span> <span class="attr">resultType</span>=<span class="string">"com.ben.entity.ClazzBean"</span>&gt;</span></span><br><span class="line">        select * from test where id = #{id}</span><br><span class="line">    <span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">mapper</span>&gt;</span></span><br></pre></td></tr></tbody></table></figure>
<p>Tips:</p>
<ul>
<li>每一个<code>mapper</code>元素对应的命名空间即接口的类包路径，给定命名空间后，该<code>mapper</code>元素将为该指定接口实现所有SQL逻辑映射</li>
<li><code>select</code>元素中的<code>id</code>属性用于执行实现接口中的相应成员函数，而<code>resultType</code>属性指定SQL语句执行后的返回类型，注意该返回类型需要使用绝对类型名</li>
<li>在标签体中，使用<code>#{}</code>表达式获取具体的函数参数，可通过<strong>函数参数名</strong>和<strong>参数下标</strong>两种方法指定参数</li>
</ul>
<h3 id="获取mapper接口实例">获取Mapper接口实例</h3>
<p>通过<code>selSession</code>实例中的<code>getMapper(Mapper.class)</code>函数，我们将获得该Mapper映射接口的实现，从此便可以调用该接口中的具体函数</p>
<figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">ClazzMapper</span> <span class="variable">clazzMapper</span> <span class="operator">=</span> sqlSession.getMapper(ClazzMapper.class);</span><br></pre></td></tr></tbody></table></figure>
<blockquote>
<p>类型的<code>class</code>属性</p>
<p>在java中，每一个类型都有一个对应的Class对象，该对象包含关于这个类型的<strong>元数据</strong>，即这个类的名称、父类、构造函数、成员方法、实现的接口等等，在<code>getMapper</code>函数中通过获取Mapper映射接口的<code>Class</code>对象，我们可以获取该接口的具体逻辑实现</p>
</blockquote>
<h2 id="ide-note">IDE Note</h2>
<blockquote>
<p>Idea Note:在项目结构设置中，我们可以在文件 -&gt; 项目框架 -&gt;
模块中对项目各文件夹进行设置，不同性质的文件夹在其中新建文件时<strong>选项不同</strong></p>
<ul>
<li>设置<code>src</code>文件夹为普通文件夹（不设置任何特殊属性）</li>
<li><code>src/main/resourecs</code>为资源文件夹</li>
<li><code>target</code>为排除文件夹</li>
<li><code>src/main/java</code>为源文件夹，源代码所在处</li>
</ul>
<p><img src="https://i.postimg.cc/RVzvzWZN/image.jpg" style="zoom:50%;"></p>
<p>注意在源文件夹中，如果右键选择创建一个.java文件，并试图在空文件中编辑一个新的类型时，Idea并不会认为这是一个java类型，并且避免对该类型代码的静态语法检查。区别于使用右键
-&gt;
创建java类型得到的文件，虽然和前者一样都是.java后缀的文件，但是Idea会显式发现这是一个java类型，并对其进行语法检查和标注</p>
</blockquote>

    </div>

    
    
    

      <footer class="post-footer">
          
          <div class="post-tags">
              <a href="/tags/Java/" rel="tag"><i class="fa fa-tag"></i> Java</a>
              <a href="/tags/SQL/" rel="tag"><i class="fa fa-tag"></i> SQL</a>
              <a href="/tags/%E5%90%8E%E7%AB%AF/" rel="tag"><i class="fa fa-tag"></i> 后端</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2024/06/29/%E7%94%B5%E5%AD%90%E6%8A%80%E6%9C%AF%E5%AE%9E%E4%B9%A0%EF%BC%88%E8%80%83%E5%89%8D%E9%A2%84%E4%B9%A0%E7%89%88%EF%BC%89/" rel="prev" title="电子技术实习">
      <i class="fa fa-chevron-left"></i> 电子技术实习
    </a></div>
      <div class="post-nav-item">
    <a href="/2024/07/03/SQL/" rel="next" title="SQL">
      SQL <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%B7%BB%E5%8A%A0%E4%BE%9D%E8%B5%96"><span class="nav-number">1.</span> <span class="nav-text">添加依赖</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E9%85%8D%E7%BD%AEmybatis"><span class="nav-number">2.</span> <span class="nav-text">配置MyBatis</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#sql%E9%AB%98%E7%BA%A7%E6%98%A0%E5%B0%84"><span class="nav-number">3.</span> <span class="nav-text">SQL高级映射</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%BC%9A%E8%AF%9D%E8%BF%9E%E6%8E%A5"><span class="nav-number">3.1.</span> <span class="nav-text">会话连接</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%8E%B7%E5%8F%96%E4%BC%9A%E8%AF%9D%E5%AE%9E%E4%BE%8B"><span class="nav-number">3.2.</span> <span class="nav-text">获取会话实例</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#mapper%E6%8E%A5%E5%8F%A3%E5%AE%9E%E7%8E%B0"><span class="nav-number">3.3.</span> <span class="nav-text">Mapper接口实现</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%8E%B7%E5%8F%96mapper%E6%8E%A5%E5%8F%A3%E5%AE%9E%E4%BE%8B"><span class="nav-number">3.4.</span> <span class="nav-text">获取Mapper接口实例</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#ide-note"><span class="nav-number">4.</span> <span class="nav-text">IDE Note</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Benson"
      src="/images/ME.ico">
  <p class="site-author-name" itemprop="name">Benson</p>
  <div class="site-description" itemprop="description">Share my thoughts with you</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">17</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">7</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">25</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/ZhouBencheng" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;ZhouBencheng" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:zhoubencheng2022@163.com" title="E-Mail → mailto:zhoubencheng2022@163.com" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2024</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Benson</span>
</div>

<!--
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://pisces.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Pisces</a> 强力驱动
  </div>
-->

        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>


  <script defer src="/lib/three/three.min.js"></script>
    <script defer src="/lib/three/canvas_lines.min.js"></script>


  













<script>
if (document.querySelectorAll('pre.mermaid').length) {
  NexT.utils.getScript('//cdn.jsdelivr.net/npm/mermaid@8/dist/mermaid.min.js', () => {
    mermaid.initialize({
      theme    : 'dark',
      logLevel : 3,
      flowchart: { curve     : 'linear' },
      gantt    : { axisFormat: '%m/%d/%Y' },
      sequence : { actorMargin: 50 }
    });
  }, window.mermaid);
}
</script>


  

  

  

</body>
</html>
